package com.zyh.flink.day02.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class KeyByTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> dataStream = environment.fromElements("hello world","hello baizhi","hello flink");
        SingleOutputStreamOperator<Object> wordsDataStream = dataStream.flatMap(new FlatMapFunction<String, Object>() {
            @Override
            public void flatMap(String s, Collector<Object> collector) throws Exception {
                String[] words = s.split("\\s+");
                for (String word : words) {
                    collector.collect(word);
                }
            }
        });

//        wordsDataStream.print();
        KeyedStream<Object, Object> keyedStream = wordsDataStream.keyBy(word -> word);
        keyedStream.print();
        environment.execute("KeyByJob");
    }
}
